<?php

class Hyip1Controller extends Controller
{
	public function filters()
	{
		return array(
			'accessControl', // perform access control for CRUD operations
		);
	}

	public function accessRules(){
	    return array(
	        array('allow', // allow authenticated users to access all actions
				'users'=>array('@'),
			),
	        array('deny', // deny anything else
	              'users'=>array('*'),
	        ),
	    );
	}

    public function actionIndex()
    {   
            $this -> render('index',array(	

            ));
    }

    public function actionSearch()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = new Hyip();

            $data = $hyip -> search();

            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
    }

    public function actionCreate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $this -> save("create");			
        }
    }
    
    public function actionUpdate()
    {
        if(Yii::app()->request->isPostRequest)
        {
			$this -> save("update");					
        }
    }
    
    public function actionUpdateRate()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = Hyip::model() -> findByPk($_POST['id']);
            $hyip -> rate = $_POST['rate'];

            if($hyip -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionDelete()
    {
        if(Yii::app()->request->isPostRequest)
        {
			$model = $_POST['models'][0];
					
			$hyip = new Hyip();
            
             if ($hyip ->deleteHyip($model['id'])) {
                echo json_encode(array("status" => 'success'));
            } else {
                echo json_encode(array("status" => 'failure'));
            }			
        }
        
        return;
    }
    
    public function actionUpdateLastPaid()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = Hyip::model() -> findByPk($_POST['id']);
            $format = "Y-m-d";
            $hyip -> lastPaidDate = date("Y-m-d H:i:s");
            
            if($hyip -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionUpdateLastWithdraw()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = Hyip::model() -> findByPk($_POST['id']);
            $format = "Y-m-d";
            $hyip -> lastWithdrawDate = date("Y-m-d H:i:s");
            
            if($hyip -> save())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionGetPlan()
    {
        $id = $_GET['id'];
        $item = Hyip::model() -> findByPk($id);
        
        echo $item -> plan;
    }
    
	public function actionView()
    {
        $id = $_GET['id'];
		$model = new Hyip();
        $item = $model -> getItem($id);
        
		$this -> renderPartial('_view',array(	
			'item' => $item
		));        
    }
	
    public function actionGetItem()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $id = $_POST['id'];
            $hyip = new Hyip();
            $item = $hyip -> getItem($id);
            
            echo json_encode($item);
        }
        
        return;
    }
    
    public function actionGetMonitor()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = new Hyip();

            $data = $hyip -> getMonitor($_POST['id']);

            echo json_encode(array("results" => $data[0], "total" => $data[1]));
            return;
        }
    }
    
    public function actionAddMonitor()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = new Hyip();

            if($hyip -> addMonitor($_POST['hyipID'], $_POST['monitorID']))
            {
                echo json_encode(array("status" => 'success'));
            }
            else {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionDeleteMonitor()
    {
        if(Yii::app()->request->isPostRequest)
        {
            $hyip = new Hyip();

            if($hyip -> deleteMonitor($_POST['hyipID'], $_POST['monitorID']) > 0)
            {
                echo json_encode(array("status" => 'success'));
            }
            else {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
    
    public function actionGetMonitorValues()
    {
        $monitor = new Monitor();

        $data = $monitor -> getAllValue();

        echo json_encode($data);
    }
    
    
    public function actionGetInvestDomain()
    {
        $hyip = new Hyip();
        $items = $hyip -> getItemsByStatus();

        echo json_encode($items);        
    }           
	
	public function actionGetActiveDomain()
    {
        $items = Hyip::model() -> getActiveDomain();

        echo json_encode($items);        
    } 
	
	public function save($action = "create")
	{
		$data = $_POST['models'][0];
		if($action == "create")
		{
			$model = new Hyip();			
			$model->setData($data);
		}
		else
		{
			$model = Hyip::model() -> findByPk($data['id']);
			unset($data['id']);
			$model -> setData($data);
		}		
		
		$format = "Y-m-d";
		
		if($model -> startDate != "")
			$model -> startDate = date($format, strtotime(str_replace("/", "-", $model -> startDate)));
		else
			$model -> startDate = null;
		
		if($model -> endDate != "")
			$model -> endDate = date($format, strtotime(str_replace("/", "-", $model -> endDate)));
		else
			$model -> endDate = null;
		
		if($model -> startDomain != "")
			$model -> startDomain = date($format, strtotime(str_replace("/", "-", $model -> startDomain)));	
		else
			$model -> startDomain = null;
		
		if($model -> endDomain != "")
			$model -> endDomain = date($format, strtotime(str_replace("/", "-", $model -> endDomain)));		
		else
			$model -> endDomain = null;
		
		if($model -> joinedDate != "")
			$model -> joinedDate = date($format, strtotime(str_replace("/", "-", $model -> joinedDate)));								
		else
			$model -> joinedDate = null;				
		
		if($model -> save())
		{				
			echo json_encode(array("status" => 'success'));
		}
		else
		{
			echo json_encode(array("status" => 'failure'));
		}   
	}
        
    public function actionRefreshLifeTime()
    {
        if(Yii::app()->request->isPostRequest)
        {                        
            if(Hyip::model() -> refreshLifeTime())
            {
                echo json_encode(array("status" => 'success'));
            }
            else
            {
                echo json_encode(array("status" => 'failure'));
            }
        }
    }
}